package org.wangchaocheng.cloudgallery.authentication.strategy;

import java.util.Objects;

import cn.dev33.satoken.secure.BCrypt;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.wangchaocheng.cloudgallery.commond.authentication.AuthenticationCmd;
import org.wangchaocheng.cloudgallery.dto.user.UserAuthenticationDTO;
import org.wangchaocheng.cloudgallery.user.gateway.UserAuthenticationGateway;

/**
 * 密码认证策略
 *
 * @author wangchaocheng
 * @date 2025/04/22
 */
@Component("PasswordAuthenticationStrategy")
@RequiredArgsConstructor
public class PasswordAuthenticationStrategy implements AuthenticationStrategy {

    private final UserAuthenticationGateway userAuthenticationGateway;

    @Override
    public boolean authenticate(AuthenticationCmd cmd) {
        // 查询认证信息
        UserAuthenticationDTO userAuthentication = userAuthenticationGateway.findByIdentifier(cmd.getIdentifier());
        if (Objects.isNull(userAuthentication)) {
            return false;
        }
        // 校验密码
        return BCrypt.checkpw(cmd.getCredential(), userAuthentication.getCredential());
    }

    @Override
    public String getAuthType() {
        return "";
    }
}
